package com.nbst.comnutil;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * @author wangwt
 * @description MD5加密
 * @since 2020-07-24
 **/
public class MD5Util {

    /**
     * MD5加密
     * @param value
     * @return
     */
    public static String encode(String value){
        try {
            //1. 获得md5加密算法工具类
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            //2. 加密的结果为十进制
            byte[] md5Bytes = messageDigest.digest(value.getBytes());
            //3. 将md5加密算法值转化为16进制
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<md5Bytes.length;i++) {
                sb.append(String.format("%02x", md5Bytes[i]));
            }
            return sb.toString().toUpperCase();

        } catch (Exception e) {
            //如果产生错误则抛出异常
            throw new RuntimeException(); 
        }
    }


    // public static void main(String[] args) {
    //     String systemCode="testCode";
    //     String sercet = "123456";
    //     String method = "startWork";
    //     Long timestamp = System.currentTimeMillis();
    //     System.out.println(timestamp);
    //     String nowSign = MD5Util.encode(systemCode + sercet + method + timestamp);
    //     System.out.println(nowSign);
    // }
}
